#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
数据库迁移脚本 - 更新现有数据库结构
"""

import sqlite3
from pathlib import Path
from config import Config

def migrate_database():
    """迁移数据库到最新结构"""
    db_path = Config.DATABASE_PATH
    
    print(f"🔄 开始迁移数据库: {db_path}")
    
    with sqlite3.connect(db_path) as conn:
        cursor = conn.cursor()
        
        try:
            # 检查templates表是否存在image_path列
            cursor.execute("PRAGMA table_info(templates)")
            columns = [column[1] for column in cursor.fetchall()]
            
            if 'image_path' not in columns:
                print("📋 添加image_path列到templates表...")
                cursor.execute("ALTER TABLE templates ADD COLUMN image_path TEXT")
                
            if 'original_filename' not in columns:
                print("📋 添加original_filename列到templates表...")
                cursor.execute("ALTER TABLE templates ADD COLUMN original_filename TEXT")
            
            conn.commit()
            print("✅ 数据库迁移完成")
            
        except Exception as e:
            print(f"❌ 迁移失败: {e}")
            conn.rollback()
            raise

if __name__ == '__main__':
    migrate_database()
